<?php
// @formatter:off
/**
 * @file p.templates.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: p.templates.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/tools/p.templates.php $
 */

define('__INDEX__', true);

require_once dirname(dirname(__FILE__)).'/config/config.php';

if(defined("WC_NOT_INSTALLED")) {
	header("Location: ../");
} else {
	function getTree($root, &$templates) {
		$directory	= dir($root);
		while($path=$directory->read()) {
			$fullPath = "{$root}/{$path}";

			if($path[0] == ".") {
				continue;
			} elseif(is_dir($fullPath)) {
				getTree($fullPath, $templates);
			} elseif(preg_match('/(\.html$|\.js$)/', $path)) {
				$templates[$fullPath]	= $fullPath;
			}
		}
		$directory->close();
	}

	if(!isset($_REQUEST['group']) && $wcDefaults['group'] !== false) {
		$_REQUEST['group'] = $wcDefaults['group'];
	}

	$_REQUEST["action"] = WC_ACTION_NOTHING;

	require_once "{$wcPaths['includes-directory']}/dynamic.conf.php";
	require_once "{$wcPaths['includes-directory']}/tools.php";

	startTheme();

	$templatesRoot	= $wcTheme->parser()->templatesRoot();
	$templates	= array();
	getTree($templatesRoot, $templates);
	ksort($templates);

	$len = strlen($templatesRoot) + 1;
	foreach($templates as $key => $value) {
		$templates[substr($value, $len)] = $value;
		unset($templates[$key]);
	}

	$title = "wcomix: Theme '{$wcTheme->name()}'";
	if(isset($_REQUEST["template"]) && isset($templates[$_REQUEST["template"]])) {
		$title.= "Template: '{$_REQUEST["template"]}'";
	}
?>
<html>
	<head>
		<title><?php echo $title; ?></title>

		<script type="text/javascript" src="../includes/jquery/1.8.0/jquery-1.8.0.min.js"></script>
		<script type="text/javascript" src="../install/install.js"></script>

		<script type="text/javascript" src="../includes/SyntaxHighlighter/scripts/shCore.js"></script>
		<script type="text/javascript" src="../includes/SyntaxHighlighter/scripts/shBrushWCParser.js"></script>
		<link type="text/css" rel="stylesheet" href="../includes/SyntaxHighlighter/styles/shCoreEclipse.css"/>
		<link type="text/css" rel="stylesheet" href="../includes/SyntaxHighlighter/styles/shThemeEclipse.css"/>
		<script type="text/javascript">
			SyntaxHighlighter.all();
		</script>

		<link rel="stylesheet" type="text/css" href="../install/install.css"/>
		<style type="text/css">
			.syntaxhighlighter {
				font-size: small !important;
			}
			.syntaxhighlighter .toolbar {
				display: none !important;
			}
		</style>
	</head>
	<body>
		<span class="PageTitle"><?php echo "Theme: '{$wcTheme->name()}'"; ?></span>
		<div class="Section">
			<div class="Paragraph">
				<ul>
<?php
$spacer = "\t\t\t\t\t";
foreach($templates as $name => $path) {
	if(isset($_REQUEST["template"]) && $name == $_REQUEST["template"]) {
		echo "{$spacer}<li><a href=\"?\">{$name}</a>";
		echo "\n{$spacer}\t<pre class=\"brush: wcparser;\">\n";
		foreach(file($templates[$_REQUEST["template"]]) as $line) {
			echo htmlentities($line);
		}
		echo "{$spacer}</pre>\n";
	} else {
		echo "{$spacer}<li><a href=\"?template={$name}\">{$name}</a>";
	}
	echo "</li>\n";
}
?>
				</ul>
			</div>
		</div>
	</body>
</html>
<?php
}
?>
